package com.jjyu.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jjyu.entity.OrderItem;
import com.jjyu.entity.PointsLog;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Param;

@Mapper
public interface PointsLogMapper extends BaseMapper<PointsLog> {

    // 根据用户 ID 查询总积分
    @Select("SELECT SUM(points_earned) FROM points_log WHERE user_id = #{userId}")
    Integer selectTotalPointsByUserId(@Param("userId") Integer userId);

    // 添加新的积分日志记录，基于订单项信息
    @Insert("INSERT INTO pointslog(user_id, points_earned, order_id, item_name, item_price, created_at) " +
            "VALUES(#{userId}, #{pointsEarned}, #{orderItem.orderId}, #{orderItem.itemName}, #{orderItem.price}, CURRENT_TIMESTAMP)")
    int insertPointsLogFromOrderItem(@Param("userId") Integer userId,
                                     @Param("pointsEarned") Integer pointsEarned,
                                     @Param("orderItem") OrderItem orderItem);
}
